﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class UserControl_Role_updateQuyenNguoidung : System.Web.UI.UserControl
{
#region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DAQuyen objQuyen;
    DALevel objLevel;
    DAChucNang objChucNang;
    DANguoidung objNguoidung;
    DALog objLog;
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
#endregion
#region "Private methods"
    
    // Hien thi Danh sach truong nhom
    private void loadtruongnhom()
    {
        DataSet dsNguoiDungView = objNguoidung.loadQuyenNguoiDungByQuyen(4,intUserID);
        for (int i = 0; i < dsNguoiDungView.Tables[0].Rows.Count; i++)
        {
            this.lbParent.Items.Add(new ListItem(dsNguoiDungView.Tables[0].Rows[i]["HoTen"].ToString(), dsNguoiDungView.Tables[0].Rows[i]["ID"].ToString()));
        } 
    }
    //private void loadTVTSganquyen()
    //{
    //    int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
    //    DataSet dsTVTSganquyen = objNguoidung.loadQuyenNguoiDungByNguoidungcaptren(5, intnguoidungcaptren);
    //    for (int i = 0; i < dsTVTSganquyen.Tables[0].Rows.Count; i++)
    //    {
    //        this.lbSub.Items.Add(new ListItem(dsTVTSganquyen.Tables[0].Rows[i]["HoTen"].ToString(), dsTVTSganquyen.Tables[0].Rows[i]["ID"].ToString()));
    //    } 
    //}
    private void loadTVTSchuaganquyen()
    {
        //int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
        DataSet dsTVTSchuaganquyen = objNguoidung.loadQuyenNguoiDungByNguoidungcaptren(5, 0);
        for (int i = 0; i < dsTVTSchuaganquyen.Tables[0].Rows.Count; i++)
        {
            this.lbchuaganquyen.Items.Add(new ListItem(dsTVTSchuaganquyen.Tables[0].Rows[i]["HoTen"].ToString(), dsTVTSchuaganquyen.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
    /// <summary>
    /// Editted by LinhDH
    /// </summary>
    private void loadTVTSganquyen()
    {
        int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
        DataSet dsTVTSganquyen = objNguoidung.loadQuyenNguoiDungByNguoidungcaptren(0, intnguoidungcaptren);
        for (int i = 0; i < dsTVTSganquyen.Tables[0].Rows.Count; i++)
        {
            this.lbSub.Items.Add(new ListItem(dsTVTSganquyen.Tables[0].Rows[i]["HoTen"].ToString(), dsTVTSganquyen.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
    
    /// <summary>
    ///Edit by LinhDH
    ///// Load toàn bộ người dùng chưa đc phân quyền, không theo người dùng cấp trên
    /// </summary>
    private void loadAllTVTSchuaganquyen()
    {
        //int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
        DataSet dsTVTSchuaganquyen = objNguoidung.loadQuyenNguoiDungByNguoidungcaptren(0, 0);
        for (int i = 0; i < dsTVTSchuaganquyen.Tables[0].Rows.Count; i++)
        {
            this.lbchuaganquyen.Items.Add(new ListItem(dsTVTSchuaganquyen.Tables[0].Rows[i]["HoTen"].ToString(), dsTVTSchuaganquyen.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
    private void loadTVTSchuaganquyenTheoParentId(int ip_i_parent_id)
    {
        DataSet dsTVTSchuaganquyen = objNguoidung.loadQuyenNguoiDungByNguoidungcaptren(ip_i_parent_id, 0);
        for (int i = 0; i < dsTVTSchuaganquyen.Tables[0].Rows.Count; i++)
        {
            this.lbchuaganquyen.Items.Add(new ListItem(dsTVTSchuaganquyen.Tables[0].Rows[i]["HoTen"].ToString(), dsTVTSchuaganquyen.Tables[0].Rows[i]["ID"].ToString()));
        }
    }
    //Delete QuyenNguoiDung
    private void DeleteQuyenNguoiDung()
    {
        for (int i = 0; i < this.lbSub.Items.Count; i++)
        {
            int intSub = Convert.ToInt32(lbSub.Items[i].Value);
            int intQuyenID = 4;
            int intCheck = objNguoidung.removeQuyenNguoiDungbyQuyen(intQuyenID, intSub);
            if (intCheck > 0)
            {
                this.lblMess.Text = "";
            }
            else
            {
                this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
            }       
        }         
    }
#endregion
#region "Events handle"
    private void CheckUserModule(int UserID, int ModuleID)
    {

    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objQuyen = new DAQuyen(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objChucNang = new DAChucNang(sqlConnec);
        objLog = new DALog(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        intModuleID = Convert.ToInt32(Request.QueryString["ChucNang"].Trim());
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            CheckUserModule(intUserID, intModuleID);
           // loadNhomID(this.ddlNhomID);  
            loadtruongnhom();
            loadAllTVTSchuaganquyen();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //DeleteQuyenNguoiDung();
            //int intQuyenID =5;
            string strNguoiDungIDs = "";
            if (lbParent.SelectedItem != null)
            {
                int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
                //Level            
                for (int i = 0; i < this.lbSub.Items.Count; i++)
                {
                    strNguoiDungIDs += lbSub.Items[i].Value + ",";
                }
                int intChẹck = objNguoidung.updateTruongNhomNguoiDung(strNguoiDungIDs,intnguoidungcaptren);
                if (intChẹck > 0)
                {
                    this.lblMess.Text = "Cập nhật thành công bản ghi";
                    Session["UserID"] = intUserID;
                    objLog.insertLog(0, 0, 6, intUserID);
                }
                else if (intChẹck == 0)
                {
                    this.lblMess.Text = "Bản ghi này đã có trong dữ liệu!";
                }
                else
                {
                    this.lblMess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
                }
            }
            else
            {
                this.lblMess.Text = "Bạn phải chọn Danh sách trưởng nhóm!";
            }
        }
    }
    protected void lbParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        int intnguoidungcaptren = Convert.ToInt32(lbParent.SelectedItem.Value);
        this.lbSub.Items.Clear();
        loadTVTSganquyen();
        this.lbchuaganquyen.Items.Clear();
        loadAllTVTSchuaganquyen();
    }

    protected void uxLevelLeftBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (lbchuaganquyen.Items.Count > 0 && lbchuaganquyen.SelectedItem != null)
        {
            ListItem selectedItem = lbchuaganquyen.SelectedItem;
            selectedItem.Selected = false;
            lbSub.Items.Add(selectedItem);
            lbchuaganquyen.Items.Remove(selectedItem);
        }
    }
    protected void uxLevelLeftAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.lbchuaganquyen.Items)
        {
            this.lbSub.Items.Add(ltTemp);
        }
        this.lbchuaganquyen.Items.Clear();
    }
    protected void uxLevelRightBtn_Click(object sender, ImageClickEventArgs e)
    {
        while (lbSub.Items.Count > 0 && lbSub.SelectedItem != null)
        {
            ListItem selectedItem = lbSub.SelectedItem;
            selectedItem.Selected = false;
            lbchuaganquyen.Items.Add(selectedItem);
            lbSub.Items.Remove(selectedItem);
        }
    }
    protected void uxLevelRightAllBtn_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem ltTemp in this.lbSub.Items)
        {
            this.lbchuaganquyen.Items.Add(ltTemp);
        }
        this.lbSub.Items.Clear();
    }
#endregion

}
